﻿#pragma once
#include <cstring>
template <class T>
class Vector
{
public:
	Vector(int capacity = 4)
		:_data(new T[capacity])
		, _size(0)
		, _capacity(capacity)
	{}

		~Vector()
	{
		delete[] _data;
		_size = 0;
		_capacity = 0;
	}

	void PushBack(T data);

	T& operator[](size_t pos);

	size_t Size();

	void PopBack();
private:
	T* _data;
	size_t _size;
	size_t _capacity;
};
// 注意：类模板中函数放在类外进行定义时，需要加模板参数列表

template <class T>//类模板
void Vector<T>::PushBack(const T data)
{
	if (_capacity == _size)
	{
		T* _tmp = new T[_capacity * 2];
		memcpy(_tmp, _data,sizeof(T)*_capacity);
		_data = _tmp;
		_capacity *= 2;
	}
	_data[_size] = data;
	_size++;
}

template <class T>
T& Vector<T>::operator[](size_t pos)
{
	return _data[pos];
}


template <class T>
size_t Vector<T>::Size()
{
	return _size;
}

template <class T>
void Vector<T>::PopBack()
{
	_size--;
}